<template>
	<div class="system-menu-container layout-pd">
		<el-card shadow="hover">
			<div class="system-menu-search mb15" v-auth="'menu:add'">
				<el-button type="primary" size="default" class="ml10" @click="onOpenAddMenu">
					<el-icon><ele-Plus /></el-icon>
					添加
				</el-button>
			</div>
			<el-table
				border
				:data="state.tableData.data"
				v-loading="state.tableData.loading"
				style="width: 100%"
				row-key="path"
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
			>
				<el-table-column label="菜单名称" show-overflow-tooltip>
					<template #default="scope">
						<SvgIcon :name="scope.row.meta.icon" />
						<span class="ml10">{{ scope.row.meta.title }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="path" label="路由路径" show-overflow-tooltip></el-table-column>
				<el-table-column label="组件路径" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ scope.row.component }}</span>
					</template>
				</el-table-column>
        <el-table-column prop="sort" label="排序" align="center" show-overflow-tooltip width="70"></el-table-column>
				<el-table-column label="类型" align="center" show-overflow-tooltip width="100">
					<template #default="scope">
						<el-tag v-if="scope.row.menuType === 'menu'" type="success" size="small">菜单</el-tag>
						<el-tag v-if="scope.row.menuType === 'btn'" type="warning" size="small">按钮</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" show-overflow-tooltip width="230" v-auths="['menu:add','menu:edit','menu:del']">
					<template #default="scope">
						<el-button size="small" type="primary" v-auth="'menu:add'" @click="onOpenAddMenu('add', scope.row)">添加下级</el-button>
						<el-button size="small" type="warning" v-auth="'menu:edit'" @click="onOpenEditMenu('edit', scope.row)">编辑</el-button>
						<el-button size="small" type="danger" v-auth="'menu:del'" @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<AddMenu ref="menuDialogRef" @refresh="getTableData()" :menuList="state.tableData.data" />
	</div>
</template>

<script setup lang="ts" name="systemMenu">
// 后台接口
import { systemApi } from '/@/api/system/index';

// 引入组件
const AddMenu = defineAsyncComponent(() => import('./modal/addMenu.vue'));

// 定义变量内容
const menuDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		loading: true,
	},
});

// 获取菜单列表
const getTableData = async () => {
	state.tableData.loading = true;
	let res = await systemApi().getMenuList()
	state.tableData.data = res.datas;
	state.tableData.loading = false;
};
// 打开新增菜单弹窗
const onOpenAddMenu = (type: string, row: any) => {
	menuDialogRef.value.openDialog(type, row);
};
// 打开编辑菜单弹窗
const onOpenEditMenu = (type: string, row: any) => {
	menuDialogRef.value.openDialog(type, row);
};
// 删除当前行
const onTabelRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除路由：${row.path}, 是否继续?`, '提示', {
		confirmButtonText: '删除',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			const res = await systemApi().delMenu({id: row.id});
			if(res.success){
				getTableData();
				ElMessage.success("删除成功");
			}else{
				ElMessage.error("删除失败");
			}
		})
		.catch(() => {});
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
